# vim: filetype=sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

# Variables we'll use to name our zones
# The first two zones are created by setup,
# then destroyed in zones_004_pos - or cleanup.
ZONE="${ZONE:=zone}"
ZONE2="${ZONE2:=zonetwo}"

# These zones are created and destroyed by zones_003_pos - or cleanup.
ZONE3="${ZONE3:=zonethree}"
ZONE4="${ZONE4:=zonefour}"

# A function which runs through a loop, adding a different characters
# to the stem passed as $1, to make sure we have a unique name that
# doesn't clash with an existing zone name. Once we've found a unique
# name, we echo that to stdout.
#
function find_unique_zonename { # initial name of zone
	
	NAME=$1
	COUNT=0
	while [ -z "$FOUND" ]
	do
		NAME="${NAME}${COUNT}"
		$ZONEADM -z $NAME list > /dev/null 2>&1
		if [ $? -eq 1 ]
		then
			FOUND=true
		fi
		COUNT=$(( $COUNT + 1 ))
	done
	echo $NAME
}

# Need a longer timeout for zone installation
export STF_TIMEOUT=3600

# Make sure that multiple sourcing of this script doesn't change the zone name
if [ -z "${RUNCONFIG}" ]
then
	ZONE=$(find_unique_zonename $ZONE)
	ZONE2=$(find_unique_zonename $ZONE2)
	ZONE3=$(find_unique_zonename $ZONE3)
	ZONE4=$(find_unique_zonename $ZONE4)

	log_note "zones.cfg gave us new zone names $ZONE,$ZONE2,$ZONE3,$ZONE4"

	export ZONE
	export ZONE2
	export ZONE3
	export ZONE4

	export RUNCONFIG="true"
fi
